package com.gomore.opple.module.orderpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomore.opple.R;
import com.gomore.opple.utils.BigDecimalUtils;
import com.gomore.opple.web.cgform.orderdetail.entity.TOOrderdetailEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by asus on 2017/5/19.
 */
public class GoodAdpater extends BaseAdapter {
    Context mContext;
    List<TOOrderdetailEntity> toOrderdetailEntityList;

    public GoodAdpater(Context context, List<TOOrderdetailEntity> toOrderdetailEntities) {
        mContext = context;
        toOrderdetailEntityList = toOrderdetailEntities;
    }

    @Override
    public int getCount() {
        return toOrderdetailEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return toOrderdetailEntityList == null ? 0 : toOrderdetailEntityList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.account_item, parent, false);
            viewHolder.image_icon = (ImageView) convertView.findViewById(R.id.image_icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.code = (TextView) convertView.findViewById(R.id.code);
            viewHolder.done_price = (TextView) convertView.findViewById(R.id.done_price);
            viewHolder.good_number = (TextView) convertView.findViewById(R.id.good_number);
            viewHolder.total = (TextView) convertView.findViewById(R.id.total);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TOOrderdetailEntity toOrderdetailEntity = toOrderdetailEntityList.get(position);
        if (toOrderdetailEntity != null) {
            String url = "demo";
            if (toOrderdetailEntity.getGoodsCover() != null) {
                String[] urls = toOrderdetailEntity.getGoodsCover().split(",");
                url = urls[0];
            }
            Glide.with(mContext)
                    .load(url)
                    .asBitmap()
                    .placeholder(R.mipmap.goods_place)
                    .into(viewHolder.image_icon);
            if (toOrderdetailEntity.getGoodsName() != null) {
                viewHolder.name.setText(toOrderdetailEntity.getGoodsName());
            }
            if (toOrderdetailEntity.getGoodsCode() != null) {
                viewHolder.code.setText(toOrderdetailEntity.getGoodsCode());
            }
            if (toOrderdetailEntity.getGoodsPrice() != null) {
                BigDecimal bigDecimal = BigDecimalUtils.forMate(toOrderdetailEntity.getGoodsPrice());
                viewHolder.done_price.setText("¥ " + bigDecimal.toString());
            }
            if (toOrderdetailEntity.getGoodsNum() != null) {
                viewHolder.good_number.setText("×" + toOrderdetailEntity.getGoodsNum());
            }
            if (toOrderdetailEntity.getGoodsPrice() != null && toOrderdetailEntity.getGoodsNum() != null) {
                BigDecimal bigDecimal = toOrderdetailEntity.getGoodsPrice().multiply(new
                        BigDecimal(toOrderdetailEntity.getGoodsNum()));
                BigDecimal totaiBig = BigDecimalUtils.forMate(bigDecimal);
                viewHolder.total.setText("¥" + totaiBig.toString());
            }
        }
        return convertView;
    }

    class ViewHolder {
        ImageView image_icon;
        TextView name, code, done_price, good_number, total;
    }
}
